﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Configuration;
using System.IO;

namespace Photography
{
    public partial class SiteMaster : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Path.GetFileName(Request.FilePath).Equals("Error.aspx", StringComparison.InvariantCultureIgnoreCase))
                return;
            
            if (WebConfigurationManager.AppSettings["maintenance"].Equals("true", StringComparison.InvariantCultureIgnoreCase))
                Response.Redirect("~/Error.aspx");

            if (!IsPostBack)
                log4net.LogManager.GetLogger(this.GetType()).InfoFormat("User at IP# {0} is visiting page '{1}'", Request.UserHostAddress, Request.Url);

            foreach (MenuItem item in NavigationMenu.Items)
                if (item.Value == "Gallery")
                {
                    Photography.App_Data.PhotographyDataContext context = new Photography.App_Data.PhotographyDataContext();
                    List<App_Data.GetPublicGalleriesResult> galleries = context.GetPublicGalleries().ToList();

                    foreach (App_Data.GetPublicGalleriesResult gallery in galleries)
                    {
                        MenuItem subItem = new MenuItem(gallery.Name, gallery.ID.ToString());
                        subItem.NavigateUrl = "/Gallery.aspx?id=" + gallery.ID.ToString();
                        item.ChildItems.Add(subItem);
                    }
                }
        }

        protected void GetSlideShowPhotos(object sender, LinqDataSourceSelectEventArgs e)
        {
            Photography.App_Data.PhotographyDataContext context = new Photography.App_Data.PhotographyDataContext();
            e.Result = context.GetSlideShowPhotos();
        }

        protected void GetPublicGalleries(object sender, LinqDataSourceSelectEventArgs e)
        {
            Photography.App_Data.PhotographyDataContext context = new Photography.App_Data.PhotographyDataContext();
            e.Result = context.GetPublicGalleries();
        }

        protected void GetPrivateGalleries(object sender, LinqDataSourceSelectEventArgs e)
        {
            Photography.App_Data.PhotographyDataContext context = new Photography.App_Data.PhotographyDataContext();
            e.Result = context.GetPrivateGalleries();
        }
    }
}
